package android.gpstrac.data;

import android.content.ContentValues;
import android.gpstrac.controls.providers.GPSTracContentProvider;
import android.gpstrac.data.ClientRequest.RequestType;
import android.net.Uri;
import android.provider.BaseColumns;

/**
 * 
 *
 * @author Nicolas
 *
 */
public class UserLocation implements Transmittable, DatabaseStorable
{
	//
	// CLASS AND INSTANCE DATA
	//	
	/**
	 * The user for this location entry.
	 */
	private User mUser;
	
	/**
	 * The latitude of this location entry.
	 */
	private double mLat;
	
	/**
	 * The longitude of this location entry.
	 */
	private double mLong;
	
	/**
	 * The validity of this location entry.
	 */
	private boolean mInvalid;
	
	/**
	 * The sequence number of the data.
	 */
	private int mSEQ;
	
	/**
	 * The size of the server representation.
	 */
	public static final int COORD_LEN = 12;
	
	public UserLocation(User user, double lat, double lon, boolean invalid, int seq)
	{
		mUser = user;
		mLat = lat;
		mLong = lon;
		mInvalid = invalid;
		mSEQ = seq;
	}
	
	public UserLocation(double lat, double lon)
	{
		this(null, lat, lon, false, 0);
	}
	
	public UserLocation(User user)
	{
		this(user, 0, 0, false, 0);
	}
	
	public UserLocation(boolean inval)
	{
		this(null, 0, 0, inval, 0);
	}
	
	public UserLocation()
	{
		this(false);
	}
	
	/* (non-Javadoc)
	 * @see android.gpstrac.data.Transmittable#getServerRepresentation()
	 */
	@Override
	public byte[] getServerRepresentation()
	{
		int validVal = (mInvalid)?(1):(0);
		
		int lon = (int)(mLong * 3600);
		int lat = (int)(mLat * 3600);
		
		byte data[] = new byte[]
		{
			(byte)(validVal >>> 24), (byte)(validVal >>> 16), (byte)(validVal >>> 8), (byte)validVal,
			(byte)(lon >>> 24), (byte)(lon >>> 16), (byte)(lon >>> 8), (byte)lon,
			(byte)(lat >>> 24), (byte)(lat >>> 16), (byte)(lat >>> 8), (byte)lat
		};
		
		return data;
	}
	
	/* (non-Javadoc)
	 * @see android.gpstrac.data.Transmittable#fillWithTransmittedData(java.lang.Byte[])
	 */
	@Override
	public void fillWithTransmittedData(byte[] data)
	{
		if(data.length < 12)
			return;
		
		int valid = (data[0] << 24) + ((data[1] & 0xFF) << 16) + ((data[2] & 0xFF) << 8) + (data[3] & 0xFF);
		mInvalid = (valid != 0);
		
		mLong = (int)((data[4] << 24) + ((data[5] & 0xFF) << 16) + ((data[6] & 0xFF) << 8) + (data[7] & 0xFF));
		mLat = (int)((data[8] << 24) + ((data[9] & 0xFF) << 16) + ((data[10] & 0xFF) << 8) + (data[11] & 0xFF));
		
		mLong /= 3600;
		mLat /= 3600;
	}
	
	/* (non-Javadoc)
	 * @see android.gpstrac.data.DatabaseStorable#getValueHolder()
	 */
	@Override
	public ContentValues getValueHolder()
	{
		ContentValues values = new ContentValues();
		values.put(UserLocationContent.USER, mUser.getName());
		values.put(UserLocationContent.LAT, mLat);
		values.put(UserLocationContent.LONG, mLong);
		values.put(UserLocationContent.SEQ, mSEQ);
		
		return values;
	}
	
	public void setSequence(int i)
	{
		mSEQ = i;
	}
	
	public boolean isInvalid()
	{
		return mInvalid;
	}
	
	public double getLat()
	{
		return mLat;
	}
	
	public double getLon()
	{
		return mLong;
	}
	
	public User getUser()
	{
		return mUser;
	}
	
	public static final class UserLocationContent implements BaseColumns
	{
		//
		// DATA
		//
		/**
		 * Table name for user content.
		 */
		public static final String TABLE_NAME = "locations";
		
		/**
		 * Index names for user content.
		 */
		public static final String LOC_ID = "_id";
		public static final String USER = "user";
		public static final String LAT = "latitude";
		public static final String LONG = "longitude";
		public static final String SEQ = "sequence";
		
		/**
		 * Content type
		 */
		public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.gpstrac.location";
		
		/**
		 * Content URI
		 */
		public static final Uri CONTENT_URI = Uri.parse("content://" + GPSTracContentProvider.AUTHORITY + "/locations");
	}
}
